/**   
* @Title: ShareLayout.java 
* @author zengshuai shi   
* @date 2015年8月5日 下午5:13:00 
*/
package com.onstar.cn.weight;

import com.onstar.cn.common.util.BitmapUtil;
import com.onstar.cn.common.util.CommonUtil;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View.OnClickListener;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/** 
* @ClassName: ShareLayout 
* @Description: 分享弹出框
* @author zengshuai shi
* @date 2015年8月5日 下午5:13:00 
*/
public class ShareLayout extends Button implements OnClickListener{
	private CustomImageView btn_share_wechat_friend;
	private CustomImageView btn_share_wx_moments;
//	private CustomImageView btn_share_sina;
//	private CustomImageView btn_share_sms;
	private Button btn_share_cancel;
	private View view;
	private ShareViewItemOnClick onItemclick;
	private PopupWindow popupWindow;

	public ShareLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ShareLayout(Context context) {
		super(context);
		init();
	}

	/**
	 * 
	 * @Title: init
	 * @Description: 初始化资源
	 * @return void
	 * @throws
	 */
	private void init() {
		view = inflate(getContext(), R.layout.weight_share_layout, null);
		btn_share_wechat_friend = (CustomImageView) view.findViewById(R.id.btn_share_wechat_friend);
		btn_share_wx_moments = (CustomImageView) view.findViewById(R.id.btn_share_wx_moments);
//		btn_share_sina = (CustomImageView) view.findViewById(R.id.btn_share_sina);
//		btn_share_sms = (CustomImageView) view.findViewById(R.id.btn_share_sms);
		btn_share_cancel = (Button) view.findViewById(R.id.btn_share_cancel);

		btn_share_wechat_friend.setOnClickListener(this);
		btn_share_wx_moments.setOnClickListener(this);
//		btn_share_sina.setOnClickListener(this);
//		btn_share_sms.setOnClickListener(this);
		btn_share_cancel.setOnClickListener(this);
		
		btn_share_wechat_friend.setBackgroundResource(R.drawable.weixin_friends);
		btn_share_wx_moments.setBackgroundResource(R.drawable.weixin_friends_circle);
//		btn_share_sina.setBackgroundResource(R.drawable.weixin_weibo);
//		btn_share_sms.setBackgroundResource(R.drawable.weixin_message);
		btn_share_cancel.setBackgroundResource(R.drawable.weight_cancel_btn);
		
	}

	/**
	 * 
	 * @Title: show
	 * @Description: 显示分享弹出框
	 * @return void
	 * @throws
	 */
	public void show() {
		if (popupWindow == null) {
			popupWindow = new PopupWindow(getContext());
		}
		popupWindow.setContentView(view);
		popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
		popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		popupWindow.setAnimationStyle(R.style.share_layout_animation);
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		popupWindow.setBackgroundDrawable(dw);
		// 设置layout在PopupWindow中显示的位置
		popupWindow.showAtLocation(ShareLayout.this, Gravity.BOTTOM, 0, 0);

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.btn_share_wechat_friend){
			if (onItemclick != null) {
				onItemclick.onclick(0, this);
			}
		}else if(id == R.id.btn_share_wx_moments){
			if (onItemclick != null) {
				onItemclick.onclick(1, this);
			}
		}else if(id == R.id.btn_share_sina){
			if (onItemclick != null) {
				onItemclick.onclick(2, this);
			}
		}
//		else if(id == R.id.btn_share_sms){
//			if (onItemclick != null) {
//				onItemclick.onclick(3, this);
//			}
//		}
		else{
			close();
		}

		close();
	}

	/**
	 * 
	 * @Title: close
	 * @Description: 关闭分享弹出框
	 * @return void
	 * @throws?
	 */
	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void close() {
		if (popupWindow != null&&popupWindow.isShowing()) {
			popupWindow.dismiss();
		}
		BitmapUtil.recycleImageView(btn_share_wechat_friend);
		BitmapUtil.recycleImageView(btn_share_wx_moments);
//		BitmapUtil.recycleImageView(btn_share_sina);
//		BitmapUtil.recycleImageView(btn_share_sms);
		if(btn_share_cancel != null) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				btn_share_cancel.setBackground(null);
			}else{
				btn_share_cancel.setBackgroundDrawable(null);
			}
			btn_share_cancel = null;
		}
	}

	/**
	 * 
	 * @Title: setOnItemclick
	 * @Description: 设置监听事件
	 * @return void
	 * @throws
	 */
	public void setOnItemclick(ShareViewItemOnClick onclick) {
		this.onItemclick = onclick;
	}

	/**
	 * 
	 * @ClassName: ShareViewItemOnClick
	 * @Description: 分享layout Item 点击监听
	 * @author Wanjin Chen
	 * @date 2013-11-21 下午5:12:35
	 */
	public interface ShareViewItemOnClick {
		/**
		 * 
		 * @Title: onclick
		 * @Description: 调用原来方法实现，因此这样传递参数，不影响以前的功能调用
		 * @return void
		 * @throws
		 */
		void onclick(int which, ShareLayout shareLayout);
	}

	
}
